﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using yq.IBase.Table;
using System.Reflection;

namespace CreateCode.Controllers
{

    class sss
    {
        private int _I = 0;

        public int I
        {
            get { return _I; }
            set { _I = value; }
        }
    }

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View(TableHelper.GetTableList());
        }

        public ActionResult CreateCode(string name, string typename)
        {
            return PartialView(typename, TableHelper.GetTable(name));
        }

        /// <summary>
        /// 生成配置文件 ioc
        /// </summary>
        /// <returns></returns>
        public ActionResult CreateIoConfig()
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();

            foreach (var model in TableHelper.GetTableList())
            {
                string key = model.Code.Split(char.Parse("_"))[0];

                if (dic.ContainsKey(key) == false)
                {
                    dic.Add(key, key);
                }

            }

            return PartialView(dic);
        }

        public ActionResult About()
        {
            return View();
        }

        string filepath = AppDomain.CurrentDomain.BaseDirectory + "\\bin\\";

        public ActionResult ToCode()
        {
            string File = filepath + DateTime.Now.ToString("yyyyMMddhhmmss") + "\\";
            System.IO.Directory.CreateDirectory(File);

            //view视图地址
            string[] codeteam = new string[] { "CreateDAL", "CreateEnum", "CreateIModel", "CreateModel", "CreateSqlite" };

            //文件路径
            string[] fileteam = new string[]
            { 
                "yq.Business\\Business\\", "yq.Enums\\", "yq.Business\\Model\\Base\\", "yq.Business\\Model\\Logic\\","Sqlite触发器\\" 
            };

            //文件格式
            string[] filename = new string[]
            { 
                "{0}Dal", "{0}", "I{0}", "{0}" , "Sqlite"
            };

            //文件扩展名
            string[] kzname = new string[]
            { 
                ".cs", ".cs", ".cs", ".cs" , ".sql"
            };

            for (int i = 0; i < codeteam.Count(); i++)
            {
                System.IO.Directory.CreateDirectory(File + fileteam[i]);
                foreach (var table in TableHelper.GetTableList())
                {
                    string txt = "";
                    string pathName=File + fileteam[i] + string.Format(filename[i], table.ClassName) + kzname[i];
                    
                    if (System.IO.File.Exists(pathName))
                    {
                        txt = System.IO.File.ReadAllText(pathName);
                    }
                    
                    txt += this.RenderViewToString(codeteam[i], table);
                    System.IO.File.WriteAllText(pathName, txt);
                }
            }

            return this.Content("生成成功 生成目录到：" + File);
        }
    }
}
